﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TamTreSoSinh.DBContext;

namespace TamTreSoSinh
{
    public partial class ShoppingPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadProduct();
            LoadShoppingCategory();
        }

        private void LoadShoppingCategory()
        {
            DBTamTreEntities dbcontext = new DBTamTreEntities();
            List<ShoppingCategory> listShoppingCategory = dbcontext.ShoppingCategories.Where(obj => obj.IsVisible).OrderBy(obj => obj.Priority).ToList();

            rptShoppingCategory.DataSource = listShoppingCategory;
            rptShoppingCategory.DataBind();
        }

        private void LoadProduct()
        {
            int Id;

            bool isValidId = int.TryParse(Request["Id"], out Id);
            if (!isValidId)
            {
                return;
            }

            DBTamTreEntities dbContext = new DBTamTreEntities();
            ShoppingCategory shoppingCategory = dbContext.ShoppingCategories.FirstOrDefault(obj => obj.Id == Id);
            if (shoppingCategory == null)
            {
                return;
            }

            lblTitleTop.Text = shoppingCategory.Title;
            lblTitle.Text = shoppingCategory.Title;

            List<Product> listProduct = shoppingCategory.Products.OrderBy(obj => obj.Priority).ToList();

            rptProduct.DataSource = listProduct;
            rptProduct.DataBind();
        }

        protected void rptShoppingCategory_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            ShoppingCategory shoppingCategory = (ShoppingCategory)e.Item.DataItem;
            HyperLink hpl = (HyperLink)e.Item.FindControl("hplShoppingCategory");
            hpl.NavigateUrl = @"/ShoppingCategoryPage.aspx#AnchorShopping" + shoppingCategory.Id;
            hpl.Text = shoppingCategory.Title;
        }

        protected void rptProduct_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Product product = (Product)e.Item.DataItem;
            HyperLink hpl;
            Label lbl;

            hpl = (HyperLink)e.Item.FindControl("hplProductName");
            hpl.NavigateUrl = @"/ProductDetailPage.aspx?Id=" + product.Id;
            hpl.Text = product.ProductName;

            hpl = (HyperLink)e.Item.FindControl("hplImagePath");
            hpl.NavigateUrl = @"/ProductDetailPage.aspx?Id=" + product.Id;
            hpl.ImageUrl = product.ImagePath;

            lbl = (Label)e.Item.FindControl("lblOldPrice");
            if (product.OldPrice.HasValue)
            {
                lbl.Text = string.Format("{0:#,##0}", product.OldPrice.Value).Replace(",", ".") + " VND";
                lbl.Visible = true;
            }

            lbl = (Label)e.Item.FindControl("lblCurrentPrice");
            lbl.Text = string.Format("{0:#,##0}", product.CurrentPrice).Replace(",", ".") + " VND";
        }
    }
}